package com.fy.market.strategy.service.rule.tree.treeFactory.engine.impl;

import com.fy.market.strategy.model.entity.RuleMatterEntity;
import com.fy.market.strategy.model.enums.RuleLogicCheckTypeEnum;
import com.fy.market.strategy.model.entity.tree.RuleTreeResultEntity;
import com.fy.market.strategy.model.entity.tree.valobj.RuleTreeNodeLineVO;
import com.fy.market.strategy.model.entity.tree.valobj.RuleTreeNodeVO;
import com.fy.market.strategy.model.entity.tree.valobj.RuleTreeVO;
import com.fy.market.strategy.service.rule.tree.ILogicTreeNode;
import com.fy.market.strategy.service.rule.tree.treeFactory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @author fwq
 * @desc 决策树引擎实现
 * @date 2025年09月25
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTreeNode> iLogicTreeNodeMap;

    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> iLogicTreeNodeMap, RuleTreeVO ruleTreeVO) {
        this.iLogicTreeNodeMap = iLogicTreeNodeMap;
        this.ruleTreeVO = ruleTreeVO;
    }

    /**
     * 处理决策树
     * @param ruleMatterEntity
     * @return
     */
    @Override
    public RuleTreeResultEntity process(RuleMatterEntity ruleMatterEntity) {
        // 获取决策树根节点
        log.info("规则树数据：{}", ruleTreeVO.toString());
        String nextNode = ruleTreeVO.getTreeRootRuleNode();
        // 获取决策树节点Map
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();
        ILogicTreeNode iLogicTreeNode = iLogicTreeNodeMap.get(nextNode);

        Integer resultAwardId = ruleMatterEntity.getGuaranteedAwardId() != null
                ? ruleMatterEntity.getGuaranteedAwardId()
                : ruleMatterEntity.getAwardId();

        // 循环处理决策树
        while ( null != iLogicTreeNode){
            log.info("现在处理节点：{}", nextNode);
            // 执行逻辑节点，响应结果包含放行枚举和奖品ID
            RuleTreeResultEntity ruleTreeResultEntity = iLogicTreeNode.logic(ruleMatterEntity);

            if (null != ruleTreeResultEntity.getAwardId()) resultAwardId = ruleTreeResultEntity.getAwardId();
            // 获取决策树节点对应的连线
            List<RuleTreeNodeLineVO> treeNodeLineVOList = treeNodeMap.get(nextNode).getTreeNodeLineVOList();

            // 获取下个节点
            nextNode = nextNode(ruleTreeResultEntity.getRuleLogicCheckTypeEnum().getCode(), treeNodeLineVOList);
            iLogicTreeNode = iLogicTreeNodeMap.get(nextNode);
        }

        // 返回最终结果
        return RuleTreeResultEntity.builder()
                .awardId(resultAwardId)
                .ruleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.ALLOW)
                .build();
    }

    public String nextNode(String ruleLogicCheckTypeCode, List<RuleTreeNodeLineVO> treeNodeLineVOList) {
        if (null == treeNodeLineVOList || treeNodeLineVOList.isEmpty()) return null;
        if (treeNodeLineVOList.size() < 2) return treeNodeLineVOList.get(0).getRuleNodeTo();
        for (RuleTreeNodeLineVO nodeLine : treeNodeLineVOList) {
            if (decisionLogic(ruleLogicCheckTypeCode, nodeLine)) {
                return nodeLine.getRuleNodeTo();
            }
        }
        throw new RuntimeException("决策树引擎，nextNode 计算失败，未找到可执行节点！");
    }

    public boolean decisionLogic(String ruleLogicCheckTypeCode, RuleTreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case EQUAL:
                return ruleLogicCheckTypeCode.equals(nodeLine.getRuleLimitValue());
            // 以下规则暂时不需要实现
            case GT:
            case LT:
            case GE:
            case LE:
            default:
                return false;
        }
    }
}
